package com.majie.resourceLoader;

import org.springframework.core.io.*;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class PathMatchingResourcePatternResolverDemo {

	public static void main(String[] args) throws IOException {

		Resource resource1 = new PathMatchingResourcePatternResolver().getResource("file:d:\\test.txt");
		if (resource1.exists()) {
			System.out.println(resource1.getFilename() + "的字节数是" + resource1.contentLength());

		}

		Resource resource2 = new PathMatchingResourcePatternResolver().getResource("test2.html");
		if (resource2.exists()) {
			System.out.println(resource2.getFilename() + "的字节数是" + resource2.contentLength());
			InputStream in2 = resource2.getInputStream();
			File test2HTML = resource2.getFile();
			EncodedResource encodedResource = new EncodedResource(resource2, StandardCharsets.UTF_8);
			String content = FileCopyUtils.copyToString(encodedResource.getReader());
			System.out.println(content);
		}

		Resource resource3 = new PathMatchingResourcePatternResolver().getResource("https://conyli.cc");
		if (resource3.exists()) {
			EncodedResource encodedResource = new EncodedResource(resource3, StandardCharsets.UTF_8);
			String content = FileCopyUtils.copyToString(encodedResource.getReader());
//			System.out.println(content);
		}
	}
}